﻿<%@ Page Language="VB" MasterPageFile="~/admin/admin.master" Title="Untitled Page" %>
<%@ import Namespace="System.Security.Principal" %>
<%@ import Namespace="System.Security.Permissions" %>
<%@ import Namespace="System.IO" %>

<script runat="server">
    Private Function checkpermission() As Boolean
        Dim oFp As FileIOPermission = New  _
       FileIOPermission(FileIOPermissionAccess.Write, Server.MapPath("") & "\test.txt")
        oFp.Assert()
        Try
            Dim objWriter As New IO.StreamWriter(File.Open(Server.MapPath("") & "\test.txt", IO.FileMode.Open))
            objWriter.WriteLine("Hi there!")
            objWriter.Flush()
            objWriter.Close()
            objWriter = Nothing
        Catch objA As System.Exception
            Console.WriteLine(objA.Message)
        End Try
        Return True
    End Function
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        'If Not Roles.IsUserInRole("admin") Then
        '    Response.Redirect("~/login.aspx")
        'End If
           
    End Sub
   
    Protected Sub btnBackup_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ' CopyDirectory("D:\Eigene Dateien\mastercms\entwicklung\userfiles", "D:\Eigene Dateien\mastercms\entwicklung\backups", False)
        ' CopyDirectory("D:\Eigene Dateien\mastercms\entwicklung\usercontrols", "D:\Eigene Dateien\mastercms\entwicklung\backups", False)
        ' CopyDirectory("D:\Eigene Dateien\mastercms\entwicklung\App_Data", "D:\Eigene Dateien\mastercms\entwicklung\backups", False)
        Dim strdir As String = Server.MapPath("~/")
        Dim strDirBackupfolder As String = "backups"
        Dim strDirBackupfoldertmp As String = Now.Year & Now.Month & Now.Day & Now.Hour & Now.Minute & Now.Second
        
        Dim rand = New Random()
        
        strDirBackupfolder = strDirBackupfolder & "/" & strDirBackupfoldertmp & "-" & rand.next(10000, 99999)
        
        Dim strDirBackup As String = createfolder(Server.MapPath("~/") & strDirBackupfolder)
        DirectoryCopy(Server.MapPath("~/") & "userfiles", Server.MapPath("~/") & strDirBackupfolder, False, False)
        DirectoryCopy(Server.MapPath("~/") & "usercontrols", Server.MapPath("~/") & strDirBackupfolder, False, False)
        DirectoryCopy(Server.MapPath("~/") & "App_Data", Server.MapPath("~/") & strDirBackupfolder, False, False)
    End Sub
    
    Private Function createfolder(ByVal strPath As String) As String
        System.IO.Directory.CreateDirectory(strPath)
        Return strPath
    End Function
    
    Private Sub DirectoryCopy(ByVal sourceDir As String, ByVal destDir As String, ByVal Recursive As Boolean, ByVal overWrite As Boolean)
        Dim dir() As DirectoryInfo
        Dim dirFile() As FileInfo
        'Dim Subdir As DirectoryInfo
        'Dim SubdirFile As FileInfo
        Dim dirFileName As FileInfo 'sub dir file name 
        Dim dirName As DirectoryInfo ' sub dir name 
        Dim SourceDirName As New DirectoryInfo(sourceDir) ' source folder name 
        ' Add trailing separators to the supplied paths if they don't exist. 
        If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            sourceDir &= System.IO.Path.DirectorySeparatorChar
        End If
        If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            destDir &= System.IO.Path.DirectorySeparatorChar
        End If

        Dim MDir As New DirectoryInfo(sourceDir) 'source 
        Dim SDir As New DirectoryInfo(destDir) ' target 
        Dim SDirChk As New DirectoryInfo(destDir.ToString() & SourceDirName.Name) ' target 

        If Recursive = False Then
            'ProgressBar1.PerformStep() 
            If SDirChk.Exists = False Then ' if target not found 
                SDirChk.Create() ' create target 
            End If
            Dim SourceDirFileName As FileInfo 'source file name 
            Dim SourceDirFile() As FileInfo ' all source file name 
            SourceDirFile = SourceDirName.GetFiles() ' get all source file name 
            For Each SourceDirFileName In SourceDirFile
                Try
                    Dim sfile As New FileInfo(SourceDirFileName.FullName)

                    If sfile.Exists = True Then
                        sfile.Attributes = FileAttributes.Normal

                        'ask dialog 
                        'Dim dlgResult As DialogResult, dlgResult2 As DialogResult
                        'Dim message As String, Caption As String
                        If overWrite = False Then
                            'message = "The folder already contains a file named ' " & sfile.Name & " ' " & vbCrLf & vbCrLf & _
                            '"Would you like to replace the existing file" & vbCrLf & _
                            'sfile.FullName & " " & sfile.LastWriteTime & vbCrLf & vbCrLf & _
                            '"with this one?" & vbCrLf & _
                            'sfile.FullName & " " & sfile.LastWriteTime
                            'Caption = "Confirm File Replace"
                            'dlgResult = MessageBox.Show(Me, message, Caption, MessageBoxButtons.YesNoCancel, _
                            'MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                            'If dlgResult = DialogResult.Yes Then
                            'End If
                            'If dlgResult = DialogResult.No Then
                            '    TaskLog("' " & sfile.FullName & " '" & " .User Cancel Overwrite")
                            '    Exit For
                            'End If
                            'If dlgResult = DialogResult.Cancel Then
                            '    TaskLog("' " & sfile.FullName & " '" & " .User Cancel Overwrite")
                            '    Exit Sub
                            'End If
                        End If
                    End If
                    'StatusBarPanel14.Text = "File : '" & SourceDirFileName.Name & "'"
                    File.Copy(SourceDirName.FullName & "\" & SourceDirFileName.Name, SDirChk.FullName & "\" & SourceDirFileName.Name, True)
                    'ProgressBar2.PerformStep()
                Catch exp As Exception
                    'TaskLog(exp.Message) 'error log 
                End Try
            Next SourceDirFileName
        End If

        dir = MDir.GetDirectories() ' get sub dir 
        'Dim dirName As DirectoryInfo ' sub dir name 
        For Each dirName In dir ' each dir name 
            Try
                Dim str As String
                If Recursive = False Then
                    str = SDir.ToString() & "\" & SourceDirName.Name & "\" & dirName.Name
                Else
                    str = SDir.ToString() & "\" & dirName.Name
                End If
                Dim TDirName As New DirectoryInfo(str) ' target folder name 
                If TDirName.Exists = False Then
                    TDirName.Create()
                End If
                dirFile = dirName.GetFiles() ' get dir file 
                'ProgressBar2.Maximum = dirFile.Length 'dir file length 
                'Label3.Text = "Copy"
                'Dim dirFileName As FileInfo 
                For Each dirFileName In dirFile 'each file name in folder 
                    Dim TdirFileName As New FileInfo(TDirName.ToString() & "\" & dirFileName.Name) 'target files 
                    If TdirFileName.Exists = True Then 'if exits then reset files 
                        TdirFileName.Attributes = FileAttributes.Normal
                        'ask dialog 
                        'Dim dlgResult As DialogResult, dlgResult2 As DialogResult
                        'Dim message As String, Caption As String
                        If overWrite = False Then
                            'message = "The folder already contains a file named ' " & TdirFileName.Name & " ' " & vbCrLf & vbCrLf & _
                            '"Would you like to replace the existing file" & vbCrLf & _
                            'TdirFileName.FullName & " " & TdirFileName.LastWriteTime & vbCrLf & vbCrLf & _
                            '"with this one?" & vbCrLf & _
                            'TdirFileName.FullName & " " & TdirFileName.LastWriteTime
                            'Caption = "Confirm File Replace"
                            'dlgResult = MessageBox.Show(Me, message, Caption, MessageBoxButtons.YesNoCancel, _
                            'MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                            'If dlgResult = DialogResult.Yes Then
                            'End If
                            'If dlgResult = DialogResult.No Then
                            '    Exit For
                            'End If
                            'If dlgResult = DialogResult.Cancel Then
                            '    Exit Sub
                            'End If
                        End If

                    End If
                    'StatusBarPanel14.Text = "Folder : '" & dirName.Name & "' File : '" & dirFileName.Name & "'"

                    'copy file 
                    File.Copy(MDir.ToString() & dirName.Name & "\" & dirFileName.Name, TDirName.FullName & "\" & dirFileName.Name, True)
                    'ProgressBar2.PerformStep()
                Next dirFileName
                DirectoryCopy(dirName.FullName, TDirName.FullName, True, overWrite)
            Catch exp As Exception
                'TaskLog(exp.Message) 'error log 
            End Try
        Next dirName
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Backupverzeichnis"></asp:Label><br />
    <br />
    <asp:Button ID="btnBackup" runat="server" OnClick="btnBackup_Click" Text="Button" />
    <br />
    <br />
    <asp:Label ID="lblStatusMessage" runat="server" Text="Label"></asp:Label>
</asp:Content>

